using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using GUI;
using System.Xml.Serialization;
using System.IO;

namespace Robot_War
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class RobotWar : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        //Test
        DragAndDrop drag;
        Texture2D tex;
        Button btn1;
        Control con;
        Textfield text;
        Textbox tbox;

        public RobotWar()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here


            //Test
            drag = new DragAndDrop(this, GraphicsDevice);
            btn1 = new Button();
            btn1.Initialize(Content);
            con = new Control();
            con.Init();
            text = new Textfield();
            tbox = new Textbox();

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            //Test
            btn1.ClickedAsset = (@"Test/Button-Highlight");
            btn1.InactiveAsset = (@"Test/Button");
            btn1.MouseOverAsset = (@"Test/Button-Highlight");
            btn1.Position = new Vector2(200, 200);
            btn1.TextPosition = new Vector2(220, 220);
            btn1.Text = "";
            btn1.FontAsset = (@"Test/Font");
            con.Buttons.Add(btn1);
            text.Position = new Vector2(200, 200);
            text.Text = "Mein Text";
            text.FontAsset = (@"Test/Font");
            text.Color = Color.White;
            con.Texts.Add(text);

            tbox = new Textbox();
            tbox.FontAsset = (@"Test/Font");
            tbox.Text = "";
            tbox.Position = new Vector2(50, 50);
            tbox.Color = Color.White;

            XmlSerializer mySerializer = new
            XmlSerializer(typeof(Control));
            // To write to a file, create a StreamWriter object.
            StreamWriter myWriter = new StreamWriter("Menue.xml");
            mySerializer.Serialize(myWriter, con);
            myWriter.Close();

            //XmlSerializer ms = new XmlSerializer(typeof(Button));
            //FileStream fs = new FileStream("myFileName.xml", FileMode.Open);
            //btn1 = (Button)ms.Deserialize(fs);
            //fs.Close();
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Entladen Sie jeglichen Nicht-ContentManager-Inhalt hier
        }

        /// <summary>
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            //Test
            if (drag.DroppedImage != null)
            {
                tex = drag.DroppedImage;
                drag.DroppedImage = null;
            }
            btn1.Update();
            tbox.Update();

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            spriteBatch.Begin();
            //Test
            if(tex != null)
            spriteBatch.Draw(tex, Vector2.Zero, Color.White);
            btn1.Draw(spriteBatch);
            tbox.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
